<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>审核页面</title>
    <script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<input id="login_user" placeholder="当前登陆用户ID">
<div style="width:100%;height:1px;margin:10px 0px;padding:0px;background-color:#D5D5D5;overflow:hidden;"></div>

<input id="group_id" placeholder="角色组ID">
<div style="width:100%;height:1px;margin:10px 0px;padding:0px;background-color:#D5D5D5;overflow:hidden;"></div>

<table>

    <h4>审核</h4>
    <tbody>
    <tr><th>TaskID</th><th id="taskId"></th></tr>
    <tr><th>审批人</th><th id="assignee"></th></tr>
    <tr><th>流程Key</th><th id="processDefinitionKey"></tr>
    <tr><th>业务名称</th><th id="businessName"></tr>
    <tr><th>审批意见</th><th><textarea id="msg"  style="width: 200px; height: 100px"></textarea></th></tr>
    <tr><th>流程图</th><th><img id="img" width="400"/></th></tr>
    <tr>
        <th><button onclick="audit(0)">同意</button></th>
        <th><button onclick="audit(1)">驳回</button></th>
    </tr>
    </tbody>
</table>


<table border="1">
    <h4>审核历史</h4>
    <thead>
        <td>id</td>
        <td>activityName</td>
        <td>activityType</td>
        <td>assignee</td>
        <td>耗时（毫秒）</td>
        <td>startTime</td>
        <td>endTime</td>
        <td>taskId</td>
        <td>备注</td>
    </thead>
    <tbody id="comment"></tbody>
</table>
<script>
    // 获取服务器的： http://xxxx
    var protocol_ip_post = window.location.protocol + "//" + window.location.host;

    // 参数
    function GetUrlParms() {
        var args = new Object();
        var query = location.search.substring(1);//获取查询串
        var pairs = query.split("&");//在逗号处断开
        for (var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');//查找name=value
            if (pos == -1) continue;//如果没有找到就跳过
            var argname = pairs[i].substring(0, pos);//提取name
            var value = pairs[i].substring(pos + 1);//提取value
            //decodeURI() 对中文进行转码
            args[argname] = decodeURI(value);//存为属性
        }
        return args;
    }

    var args = new Object();
    args = GetUrlParms();

   // var user_id= args["assignee"];
   var user_id= $("#login_user").val();
   var group_id= $("#group_id").val();

    // 如果要查找参数key:
    $("#taskId").text(args["taskId"]);
    console.log("businessName==" + decodeURI(args["assignee"]) );
    $("#businessName").text(args["businessName"]);
    $("#assignee").text(user_id);
    $("#processDefinitionKey").text(args["processDefinitionKey"]);
    $("#img").attr("src", protocol_ip_post + "/flowable/v2/processDiagram?processInstanceId=" + args["processInstanceId"] + "&executionId=" + args["executionId"]);


    function audit(e) {
        //e: 0同意 1驳回
        var success = true;
        if(e == 0){
            success = true;
        }else {
            success = false;
        }
        var obj = {
            "action":"complete",
            "localScope":false,
            "assignee": user_id,
            "variables":{success:success, groupList:group_id}
        };
        $.ajax({
            type: "POST",
            url: protocol_ip_post + "/runtime/tasks/"+args["taskId"],
            dataType: "json", //预期服务器返回数据的类型
            data: JSON.stringify(obj),
            contentType: 'application/json;charset=UTF-8',
            success: function (data) {
                console.log("data:", data);
                if(data.success){
                    alert(data.message+",结果："+JSON.stringify(data.result))
                }else {
                    alert(data.message)
                }

            }
        })
        //保存备注
        var obj1 = {
            "taskId": args["taskId"],
            "processInstanceId": args["processInstanceId"],
            "message": $("#msg").val(),
            "userId":user_id
        };
        $.ajax({
            type: "POST",
            url: protocol_ip_post + "/runtime/tasks/comment",
            dataType: "json", //预期服务器返回数据的类型
            data: JSON.stringify(obj1),
            contentType: 'application/json;charset=UTF-8',
            success: function (data) {
                console.log("data:", data);
                if(data.success){
                    alert(data.message)
                }else {
                    alert(data.message)
                }
            }
        })
    }


    //查询任务详情
    $.ajax({
        type: "GET",
        url: protocol_ip_post + "/runtime/tasks/records?processInstanceId=" + args["processInstanceId"] + "&executionId=" + args["executionId"],
        dataType: "json", //预期服务器返回数据的类型
        success: function (data) {
            console.log("records:", data);
            var dd = data.result;
            var html ="";
            for (var i in dd) {
                var comments = JSON.stringify(dd[i].comments);
                dd[i] = dd[i].historic;
                html += "<tr>";
                html += "<td>" + dd[i].id + "</td>";
                html += "<td>" + dd[i].activityName + "</td>";
                html += "<td>" + dd[i].activityType + "</td>";
                html += "<td>" + dd[i].assignee + "</td>";
                html += "<td>" + dd[i].durationInMillis + "</td>";
                html += "<td>" + dd[i].startTime + "</td>";
                html += "<td>" + dd[i].endTime + "</td>";
                html += "<td>" + dd[i].taskId + "</td>";
                html += "<td>" + comments + "</td>";
                html +="</tr>";
            }
            $("#comment").append(html)
        }
    })


</script>
</body>
</html>